package com.wkh.datastructure.stack;
//后缀表达式计算值
public class PostfixExpression {
    public static void main(String[] args) {
        String[] s = {"2","1","+","3","*"};
//        String[] s = {"4","13","5","/","+"};
        int count = compute(s);
        System.out.println(count);
    }
    public static int compute(String[] str){
        ArrayStack<Integer> stack = new ArrayStack<>(str.length);
        for (int i = 0; i < str.length; i++) {
            String c = str[i];
            if (c == "+"){
                Integer b = stack.pop();
                Integer a = stack.pop();
                stack.push(a + b);
            }else if (c == "-"){
                Integer b = stack.pop();
                Integer a = stack.pop();
                stack.push(a - b);
            }else if (c == "*"){
                Integer b = stack.pop();
                Integer a = stack.pop();
                stack.push(a * b);
            }else if (c == "/"){
                Integer b = stack.pop();
                Integer a = stack.pop();
                stack.push(a / b);
            }else {
                stack.push(Integer.parseInt(c));
            }
        }
        return stack.pop();
    }
}
